﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmThongTinTuyenXeBus : Form
    {
        private TuyenXeBusBUS txbBUS = new TuyenXeBusBUS();
        private LoTrinhBUS ltBUS = new LoTrinhBUS();
        private TuyenXeBuyt txb = new TuyenXeBuyt();
        private DataTable dt = new DataTable();
        private int y;

        public frmThongTinTuyenXeBus()
        {
            InitializeComponent();

            Load_Tuyen_Xe();
        }

        private void Load_Tuyen_Xe()
        {
            dgvTuyenXe.AutoGenerateColumns = false;
            dgvTuyenXe.DataSource = txbBUS.TuyenXeBuyt();
        }

        private void btThemMoi_Click(object sender, EventArgs e)
        {
            frmThemMoi tm = new frmThemMoi();
            tm.ShowDialog();
            Load_Tuyen_Xe();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn sửa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                y = dgvTuyenXe.CurrentCell.RowIndex;
                try
                {
                    txb.Ma_Tuyen = dgvTuyenXe.Rows[y].Cells[0].Value.ToString();
                    txb.So_Hieu_Tuyen = txtSoHieu.Text.Trim();
                    txb.Ten_Tuyen = txtTenTuyen.Text.Trim();
                    txb.Chieu_Dai_Tuyen = txtChieuDai.Text.Trim();
                    txb.TG_Bat_Dau = txtTGBatDau.Text.Trim();
                    txb.TG_Ket_Thuc = txtTGKetThuc.Text.Trim();
                    txb.TG_Gian_Cach_CD = txtCaoDiem.Text.Trim();
                    txb.TG_Gian_Cach_TD = txtThapDiem.Text.Trim();

                    txbBUS.SUA_TUYEN_XE(txb);
                    MessageBox.Show("Sửa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception) 
                {
                    MessageBox.Show("Sửa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Load_Tuyen_Xe();
            }

        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult  dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvTuyenXe.CurrentCell.RowIndex;
                    txb.Ma_Tuyen = dgvTuyenXe.Rows[y].Cells[0].Value.ToString();
                    txbBUS.XOA(txb.Ma_Tuyen);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                Load_Tuyen_Xe();
            }
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi(int y)
        {
            string id = dgvTuyenXe.Rows[y].Cells[0].Value.ToString();
            dt = txbBUS.LayDuLieuTheoID(id);
            txtSoHieu.Text = dt.Rows[0]["So_Hieu_Tuyen"].ToString();
            txtTenTuyen.Text = dt.Rows[0]["Ten_Tuyen"].ToString();
            txtChieuDai.Text = dt.Rows[0]["Chieu_Dai_Tuyen"].ToString();
            txtTGBatDau.Text = dt.Rows[0]["Bat_Dau"].ToString();
            txtTGKetThuc.Text = dt.Rows[0]["Ket_Thuc"].ToString();
            txtCaoDiem.Text = dt.Rows[0]["TG_Gian_Cach_CD"].ToString();
            txtThapDiem.Text = dt.Rows[0]["TG_Gian_Cach_TD"].ToString();
        }

        private void dgvTuyenXe_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi(y);
        }
        
        private void btSuaLoTrinh_Click(object sender, EventArgs e)
        {
            //frmLoTrinh lt = new frmLoTrinh();
            frmLoTrinh_ConDuong lt = new frmLoTrinh_ConDuong();
            y = dgvTuyenXe.CurrentCell.RowIndex;

            lt.lblMaTuyen.Text = dgvTuyenXe.Rows[y].Cells[0].Value.ToString();
            lt.ma_tuyen = dgvTuyenXe.Rows[y].Cells[0].Value.ToString();// Truyền biến giữa 2 form
            lt.dgvLoTrinh.AutoGenerateColumns = false;
            lt.dgvLoTrinh.DataSource = ltBUS.LoTrinh(lt.ma_tuyen);
            lt.ShowDialog();
            

        }
    }
}
